import { _decorator, Color, Component, instantiate, Label, Node } from 'cc';
import { Report, SideType } from '../entity/Report';
import { UserVillage } from './UserVillage';
import { ArmyLose } from './ArmyLose';
import { Resource } from './Resource';
import { Infomation } from './Infomation';
import { ReportType } from '../settings/const';
import { reportInfo_Attack } from '../entity/reportInfo_Attack';
import { ArmyCount } from './ArmyCount';
import { reportInfo_SendRes } from '../entity/reportInfo_SendRes';
import { reportInfo_SendHelp } from '../entity/reportInfo_SendHelp';
import { reportInfo_Invest } from '../entity/reportInfo_Invest';
import { Army, ArmyType } from '../entity/Army';
import { reportInfo_AttackedHelp } from '../entity/reportInfo_AttackedHelp';
import { OperationItem } from '../entity/OperationItem';
import { FightManager } from '../fight/FightManager';
import { UIManager } from '../common/UIManager';
import { ColorEnum } from '../entity/ColorEnum';
import { PrefabEnum } from '../common/PrefabEnum';
import { ReportFight } from './ReportFight';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;



@ccclass('ReportPanel')
export class ReportPanel extends Component {
    @property(Label)
    tittle: Label
    @property(UserVillage)
    userVillage: UserVillage
    @property(ArmyLose)
    armyLose: ArmyLose
    @property(ArmyCount)
    armyCount: ArmyCount
    @property(Resource)
    resource: Resource
    @property(Infomation)
    infomation: Infomation

    private report: Report
    private side: SideType

    start() {
        this.userVillage.node.removeFromParent()
        this.armyLose.node.removeFromParent()
        this.armyCount.node.removeFromParent()
        this.resource.node.removeFromParent()
        this.infomation.node.removeFromParent()
    }

    setData(report: Report, side: SideType) {
        this.report = report
        this.side = side

        switch (report.reportType) {
            case ReportType.rp_Null:
                this.rpNull()
                break
            case ReportType.rp_SendRes:
                this.sendRes()
                break
            case ReportType.rp_SendHelp:
                this.sendHelp()
                break
            case ReportType.rp_Attack:
                this.attack()
                break
            case ReportType.rp_AttackedHelp:
                this.attackedHelp()
                break
            case ReportType.rp_Invest:
                
                this.invest()
                break
            case ReportType.rp_Battle:
                this.battle()
                break
        }
    }

    rpNull(){
        if (this.side == SideType.Defense) {
            this.node.active = false
            return
        }

      
        this.tittle.node.parent.active = false
        let gameObj = instantiate(this.infomation.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(Infomation).setInvest(this.report)
    }

    sendRes() {
        if (this.side == SideType.Defense) {
            this.node.active = false
            return
        }

        let info = new reportInfo_SendRes(this.report)
        this.tittle.string = '补给物资信息'
        this.tittle.color = new Color(ColorEnum.Green) 

        let gameObj = instantiate(this.userVillage.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(UserVillage).setRes(this.report, info)

        gameObj = instantiate(this.resource.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(Resource).setData(info.wood, info.mud, info.iron, info.food, '物资详情')


    }

    sendHelp() {
        if (this.side == SideType.Defense) {
            this.node.active = false
            return
        }


        let info = new reportInfo_SendHelp(this.report)
        this.tittle.string = '支援军队信息'
        this.tittle.color = new Color(ColorEnum.Green)

        let gameObj = instantiate(this.userVillage.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(UserVillage).setHelp(this.report)

        gameObj = instantiate(this.armyCount.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(ArmyCount).setData(info)
        gameObj.getComponent(ArmyCount).setTittle('支援军队数量')

        gameObj = instantiate(this.infomation.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(Infomation).setHelp(info, '军队粮食消耗')


    }

    attack() {
        let info = new reportInfo_Attack(this.report)

        let gameObj = instantiate(this.userVillage.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(UserVillage).setData(this.report, this.side)

        gameObj = instantiate(this.armyLose.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(ArmyLose).setData(this.report, this.side, info)


        if (this.side == SideType.Attack) {
            this.tittle.string = '进攻方战报'
            this.tittle.color = new Color(ColorEnum.Red)
            gameObj = instantiate(this.resource.node)
            this.node.addChild(gameObj)
            gameObj.getComponent(Resource).setData(info.wood, info.mud, info.iron, info.food)

            gameObj = instantiate(this.infomation.node)
            this.node.addChild(gameObj)
            gameObj.getComponent(Infomation).setData(this.report, this.side, info)

        } else {
            if(this.report.content == '你的士兵没有回来'){
                this.node.active = false
                return
            }

            this.tittle.string = '防守方战报'
            this.tittle.color = new Color(ColorEnum.Blue)
            let list = info.totalHelpArmy.sList
            let lost = info.totalLostHelpArmy.sList
            for (let i = 0; i < 4; i++) {
                gameObj = instantiate(this.armyLose.node)
                this.node.addChild(gameObj)
                let a = list.slice(i * 10, (i + 1) * 10)
                let b = lost.slice(i * 10, (i + 1) * 10)
                gameObj.getComponent(ArmyLose).setHelpArmy(a, b)
            }

        }


    }

    invest() {
        let info = new reportInfo_Invest(this.report)

        let gameObj = instantiate(this.userVillage.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(UserVillage).setData(this.report, this.side)

        if (this.side == SideType.Attack) {
            this.tittle.string = '进攻方战报'
            this.tittle.color = new Color(ColorEnum.Red)

           
            if(this.report.SimZoneId == UserInfo.instance.villageData.zoneId){
                 // 被侦察
                 gameObj = instantiate(this.infomation.node)
                this.node.addChild(gameObj)
                this.report.content = '侦察了你的主村'
                gameObj.getComponent(Infomation).setInvest(this.report)
            } else {
                // 侦察

                gameObj = instantiate(this.armyLose.node)
                this.node.addChild(gameObj)
                gameObj.getComponent(ArmyLose).render(info.attackArmy, info.lostAttackArmy)
    
    
                gameObj = instantiate(this.infomation.node)
                this.node.addChild(gameObj)
                gameObj.getComponent(Infomation).setInvest(this.report)
            }

           
        } else {
            if(this.report.content == '你的士兵没有回来'){
                this.node.active = false
                return
            }

           
            if(this.report.SimZoneId == UserInfo.instance.villageData.zoneId){
                 // 被侦察
                this.node.active = false
                return
            }

            this.tittle.string = '防守方战报'
            this.tittle.color = new Color(ColorEnum.Blue)
            // gameObj = instantiate(this.resource.node)
            // this.node.addChild(gameObj)
            // gameObj.getComponent(Resource).setData(info.wood, info.mud, info.iron, info.food, '本村资源')

            gameObj = instantiate(this.armyCount.node)
            this.node.addChild(gameObj)
            gameObj.getComponent(ArmyCount).setArmy(info.defenseArmy)
            
            let list = info.totalHelpArmy.sList
            for (let i = 0; i < 3; i++) {
                gameObj = instantiate(this.armyCount.node)
                
                let a = list.slice(i * 10, (i + 1) * 10)
                let army = new Army()
                army.armyType == ArmyType.Help
                army.soldierList = a
                gameObj.getComponent(ArmyCount).setArmy(army)
                gameObj.getComponent(ArmyCount).setTittle('其他村庄的支援')
                if(army.soldierCount > 0){
                    this.node.addChild(gameObj)
                }
                
            }
        }
    }

    attackedHelp() {
        if (this.side == SideType.Defense) {
            this.node.active = false
            return
        }

        let info = new reportInfo_AttackedHelp(this.report)
        this.tittle.string = '支援军队信息'
        this.tittle.color = new Color(ColorEnum.Green)

        let gameObj = instantiate(this.userVillage.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(UserVillage).setHelp(this.report)

        gameObj = instantiate(this.armyLose.node)
        this.node.addChild(gameObj)
        gameObj.getComponent(ArmyLose).render(info.helpArmy, info.lostHelpArmy)
    }

    async battle() {
        if (this.side == SideType.Defense) {
            this.node.active = false
            return
        }
        let ui = await UIManager.instance.openUI(PrefabEnum.ReportFight)
        ui.getComponent(ReportFight).setData(this.report)

        UIManager.instance.closeUI(PrefabEnum.ReportDetail)
        return
        FightManager.instance.report = this.report
        UIManager.instance.loadScene('fight')

       
    }
}


